import {
  addTree,
  removeTree,
  getTreeList,
  getQuestionsByTypeId,
  getCommonQuestionsByTypeId,
  deleteTreeQuestions,
  updateTreeQuestion,
  getFormulaToolbar,
  updateDemoQuestion,
  saveTreeQuestionTag,
  getTreeTotalTags,
  saveTreeQuestion,
  getTreeQuesitonsHistory,
  moveTrees,
  moveTreeItem,
  getTreeQuestionByIds,
} from  '../../../service/api';
import { myMessage } from '../../../utils/formatDataSource';

export default {
  namespace: 'treeConfig2',
  state: {
    treeList: [],
    questions: {
      total: 0,
      data: [],
    },
    toolBar: [],
    response: {},
    tags: [],
    tagSaveStatus: false,
    historyQuestions: [],
    qts: [],
  },

  effects: {
    *list({ payload }, { call, put }) {
      const response = yield call(getTreeList, payload);
      yield put({
        type: 'r_list',
        payload: response,
      });
    },
    *add({ payload }, { call, put }) {
      const response = yield call(addTree, payload);
      yield put({
        type: 'r_add',
        payload: response,
      });
    },
    *remove({ payload }, { call, put }) {
      const response = yield call(removeTree, payload);
      yield put({
        type: 'r_remove',
        payload: response,
      });
    },
    *questions({ payload }, { call, put }) {
      const response = yield call(getQuestionsByTypeId, payload);
      yield put({
        type: 'r_questions',
        payload: response,
      });
    },
    *commonQuestions({ payload }, { call, put }) {
      const response = yield call(getCommonQuestionsByTypeId, payload);
      yield put({
        type: 'r_commonQuestions',
        payload: response,
      });
    },
    *deleteQuestions({ payload }, { call, put }) {
      const response = yield call(deleteTreeQuestions, payload);
      yield put({
        type: 'r_deleteQuestions',
        payload: response,
      });
    },
    *updateQuestion({ payload }, { call, put }) {
      const response = yield call(updateTreeQuestion, payload);
      yield put({
        type: 'r_updateQuestion',
        payload: response,
      });
    },
    *getFormulaToolbar({ payload }, { call, put }) {
      const response = yield call(getFormulaToolbar, payload);
      yield put({
        type: 'r_getFormulaToolbar',
        payload: response,
      });
    },
    *updateDemoQuestion({ payload }, { call, put }) {
      const response = yield call(updateDemoQuestion, payload);
      yield put({
        type: 'r_updateDemoQuestion',
        payload: response,
      });
    },
    *saveTag({ payload }, { call, put }) {
      const response = yield call(saveTreeQuestionTag, payload);
      yield put({
        type: 'r_saveTag',
        payload: response,
      });
    },
    *getTotalTags({ payload }, { call, put }) {
      const response = yield call(getTreeTotalTags, payload);
      yield put({
        type: 'r_getTotalTags',
        payload: response,
      });
    },
    *saveTreeQuestion({ payload }, { call, put }) {
      const response = yield call(saveTreeQuestion, payload);
      yield put({
        type: 'r_saveTreeQuestion',
        payload: response,
      });
    },
    *getTreeQuesitonsHistory({ payload }, { call, put }) {
      const response = yield call(getTreeQuesitonsHistory, payload);
      yield put({
        type: 'r_getTreeQuesitonsHistory',
        payload: response,
      });
    },
    *moveTrees({ payload }, { call, put }) {
      const response = yield call(moveTrees, payload);
      yield put({
        type: 'r_moveTrees',
        payload: response,
      });
    },
    *moveTreeItem({ payload }, { call, put }) {
      const response = yield call(moveTreeItem, payload);
      yield put({
        type: 'r_moveTreeItem',
        payload: response,
      });
    },
    *getTreeQuestionByIds({ payload }, { call, put }) {
      const response = yield call(getTreeQuestionByIds, payload);
      yield put({
        type: 'r_getTreeQuestionByIds',
        payload: response,
      });
    },
  },

  reducers: {
    r_list(state, action) {
      if (action.payload) {
        const { data = [] } = action.payload;
        state.treeList = data;
        data.map((d, i) => {
          d.viewContent = `第${i + 1}章-${d.content}`;
          if (d.sons) {
            d.sons.map((ds, i) => {
              ds.viewContent = `第${i + 1}节-${ds.content}`;
            });
          }
        });
      }
      return {
        ...state,
      };
    },
    r_add(state, action) {
      const response = action.payload;
      state.response = response;
      if (response.status === 0) {
        myMessage.success('知识树更新成功', 2);
      } else {
        myMessage.error('知识树更新失败', 2);
      }
      return {
        ...state,
      };
    },
    r_remove(state, action) {
      state.response = action.payload;
      return {
        ...state,
      };
    },
    r_questions(state, action) {
      const questions = action.payload.data;
      if (questions.data) {
        questions.data.map(d => (d.key = d.id));
      }
      return {
        ...state,
        questions,
      };
    },
    r_commonQuestions(state, action) {
      const questions = action.payload.data;
      if (questions.data) {
        questions.data.map(d => (d.key = d.id));
      }
      return {
        ...state,
        questions,
      };
    },
    r_deleteQuestions(state, action) {
      const { status } = action.payload;
      if (status === 0) {
        myMessage.success('删除成功', 2);
      } else {
        myMessage.error('删除失败', 2);
      }
      return {
        ...state,
      };
    },
    r_updateQuestion(state, action) {
      const { status } = action.payload;
      if (status === 0) {
        myMessage.success('更新成功', 2);
      } else {
        myMessage.error('更新失败', 2);
      }
      return {
        ...state,
      };
    },
    r_getFormulaToolbar(state, action) {
      if (action.payload) {
        state.toolBar = action.payload.data;
      }
      return {
        ...state,
      };
    },
    r_updateDemoQuestion(state, action) {
      return {
        ...state,
      };
    },
    r_saveTag(state, action) {
      if (action.payload.status === 0) {
        state.tagSaveStatus = true;
        myMessage.success('保存成功', 2);
      } else {
        state.tagSaveStatus = false;
        myMessage.error('保存失败', 2);
      }
      return {
        ...state,
      };
    },
    r_getTotalTags(state, action) {
      console.log(action.payload.data);
      if (action.payload) {
        state.tags = action.payload.data;
      }
      return {
        ...state,
      };
    },
    r_saveTreeQuestion(state, action) {
      if (action.payload.status === 0) {
        myMessage.success('保存成功', 2);
      } else {
        myMessage.error('保存失败', 2);
      }
      return {
        ...state,
      };
    },
    r_getTreeQuesitonsHistory(state, action) {
      if (action.payload) {
        state.historyQuestions = action.payload.data;
      }
      return {
        ...state,
      };
    },
    r_moveTrees(state, action) {
      return {
        ...state,
      };
    },
    r_moveTreeItem(state, action) {
      return {
        ...state,
      };
    },
    r_getTreeQuestionByIds(state, action) {
      if (action.payload) {
        state.qts = action.payload.data;
      }
      return {
        ...state,
      };
    },
  },
};
